package com.liuyucheng.idea_springboot.exception;

import com.liuyucheng.idea_springboot.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author LiuYuCheng
 * @date 2021年12月29日 16:05
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResultUtil<Object> handleNullPointerEx(Exception e){
        log.error(e.getMessage(),e);
        if(e instanceof ShiroException){
            return ResultUtil.result(401,"您没有权限访问!");
        }
        else if(e instanceof NullPointerException){
            return ResultUtil.result(500,"系统异常，空指针");
        }else if(e instanceof ArrayIndexOutOfBoundsException || e instanceof StringIndexOutOfBoundsException){
            return ResultUtil.result(500,"系统异常，下标越界");
        }

        return ResultUtil.result(500,"未知异常");
    }

    @ExceptionHandler(BaseException.class)
    public ResultUtil<Object> handleNullPointerEx(BaseException e){
        log.error("报错信息:",e);
        return ResultUtil.result("出现了BaseException");
    }
}
